package testCases;

import com.imagotag.esl.api.LedColor;
import com.imagotag.esl.api.LedPattern;
import com.imagotag.test.LabelIdProvider;
import com.imagotag.test.TestBuilder;
import com.imagotag.test.enums.LabelCounter;
import com.imagotag.test.enums.LabelSensor;
import com.imagotag.test.image.ImageGenerator;
import com.imagotag.test.image.Pattern;

import java.util.concurrent.TimeUnit;

/**
 * @projectName: endurance_test
 * @className: Fun_PeriodicTask
 * @description:
 * @author: JLONG
 * @date: 2024/10/21 16:28
 * @version: 1.0
 */
public class Fun_SingleTask {
    public static void PreCondition(TestBuilder testBuilder, LabelIdProvider labelIdProvider){
        testBuilder.waitBetweenTests("=========================<执行--计数器清零--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("Reset Counters", test -> test.resetCounters(labelIdProvider,
                        LabelCounter.RECEIVED_DATA_PACKETS_LSB,
                        LabelCounter.RECEIVED_DATA_PACKETS_MSB,
                        LabelCounter.SCANNED_CHANNELS_LSB,
                        LabelCounter.SCANNED_CHANNELS_MSB,
                        LabelCounter.TOTAL_DISPLAY_UPDATES,
                        LabelCounter.FAILED_DISPLAY_UPDATES,
                        LabelCounter.BROWNOUT_RESETS,
                        LabelCounter.RESYNCS_LSB,
                        LabelCounter.RESYNCS_MSB))
        ;
    }

    public static void singleTask(TestBuilder testBuilder, LabelIdProvider labelIdProvider, int times){
        testBuilder.waitBetweenTests("=========================<" +"第"+times+ "次执行--图片刷新--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("send random_black_white_red image", test -> test.sendImage(labelIdProvider, ImageGenerator.forPattern(Pattern.RANDOM_BLACK_WHITE_RED), 0))
                .waitBetweenTests("Wait until the display update has finished", 30, TimeUnit.SECONDS)

                .waitBetweenTests("=========================<" +"第"+times+ "次执行--LED闪灯--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("Blink LED", test -> test.basicLed(labelIdProvider, LedColor.WHITE, LedPattern.PATTERN_2, 10))
                .waitBetweenTests("Wait until the LED-flash has finished", 20, TimeUnit.SECONDS)

                .waitBetweenTests("=========================<" +"第"+times+ "次执行--传感数据查询--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("Query sensors", test -> test.querySensors(labelIdProvider,
                                LabelSensor.MCU_SUPPLY_VOLTAGE_SENSOR,
                                LabelSensor.TEMPERATURE_SENSOR,
                                LabelSensor.BATTERY_RESISTANCE_SENSOR))

                .waitBetweenTests("=========================<" +"第"+times+ "次执行--全信道扫描--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("Force channel scan", test -> test.forceChannelScan(labelIdProvider, true))
                .waitBetweenTests("Wait for channel scan finished", 15, TimeUnit.SECONDS)

                .waitBetweenTests("=========================<" +"第"+times+ "次执行--计数器查询--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("Query counters", test -> test.queryCounters(labelIdProvider,
                        LabelCounter.RECEIVED_DATA_PACKETS_LSB,
                        LabelCounter.RECEIVED_DATA_PACKETS_MSB,
                        LabelCounter.SCANNED_CHANNELS_LSB,
                        LabelCounter.SCANNED_CHANNELS_MSB,
                        LabelCounter.TOTAL_DISPLAY_UPDATES,
                        LabelCounter.FAILED_DISPLAY_UPDATES,
                        LabelCounter.BROWNOUT_RESETS,
                        LabelCounter.RESYNCS_LSB,
                        LabelCounter.RESYNCS_MSB))

                .waitBetweenTests("等待第"+times+ "次循环任务结束", 200, TimeUnit.SECONDS)
        ;
    }

    public static void preloadImage(TestBuilder testBuilder, LabelIdProvider labelIdProvider, int times){
        Pattern[] patterns = new Pattern[]{Pattern.BIT_CHECKERED_RED_BLACK, Pattern.CHECKERED_16_PIXEL_YELLOW_WHITE, Pattern.HORIZONTAL_BLACK_RED_WHITE, Pattern.VERTICAL_BLACK_YELLOW_WHITE};
        int temp = times %5;
        testBuilder.waitBetweenTests("=========================<" +"第"+times+ "次执行--图片预加载--任务>=========================", 0, TimeUnit.SECONDS)
                .registerTest("preload image to page" + temp, test -> test.sendImage(labelIdProvider, ImageGenerator.forPattern(patterns[temp]), temp))
                .waitBetweenTests("执行第"+times+ "次任务完成，等待30秒", 30, TimeUnit.SECONDS)
                ;
    }
}
